<jsp:include page="/views/includes/header.jsp" />
<jsp:useBean id="g" type="java.util.List<Models.Groupe>" scope="request" />

	<h1>Ajouter un module</h1>
	<% if (request.getAttribute("saved") != null) { %>
		<span class="success"><%= request.getAttribute("saved") %></span>
	<% } %>
		<form method="POST" action="<%= getServletContext().getContextPath() %>/modules/add">
		<div>
			<label for="nom">Nom : </label>
			<input type="text" name="nom" id="nom" value="<%= request.getParameter("nom") != null && request.getAttribute("saved") == null ? request.getParameter("nom") : "" %>" />
			<% if (request.getAttribute("err-nom") != null) {%>
				<span class="error"><%= request.getAttribute("err-nom") %></span>
			<% } %>
		</div>
		<div>
			<label for="description">Description :</label>
			<input type="text" name="description" id="description" value="<%= request.getParameter("description") != null && request.getAttribute("saved") == null ? request.getParameter("description") : "" %>" />
			<% if (request.getAttribute("err-desc") != null) { %>
				<span class="error"><%= request.getAttribute("err-desc") %></span>
			<% } %>
		</div>
		<%
		if (request.getParameter("groupe[0]") != null && request.getAttribute("saved") == null) {
			for (String p : request.getParameterMap().keySet()) {
				if (p.length() > 6 && p.substring(0, 6).equals("groupe")) {
		%>
				<div class="groupes">
					<label>Groupe : </label>
					<select name="<%= p %>">
					<% for (int i = 0; i < g.size(); i++) { %>
						<option value="<%= g.get(i).getIdGroupe() %>"<%= Integer.parseInt(request.getParameter(p)) == g.get(i).getIdGroupe() ? " selected=\"selected\"" : "" %>><%= g.get(i).getNom() %></option>
					<% } %>
					</select>
					<button name="del_groupe">Dissocier le groupe</button>
				</div>
		<%
				}
			}
		} else {
		%>
		<div class="groupes">
			<label>Groupe : </label>
			<select name="groupe[0]">
			<% for (int i = 0; i < g.size(); i++) { %>
				<option value="<%= g.get(i).getIdGroupe() %>"><%= g.get(i).getNom() %></option>
			<% } %>
			</select>
			<% if (request.getAttribute("err-grp") != null) { %>
				<span class="error"><%= request.getAttribute("err-grp") %></span>
			<% } %>
			<button name="del_groupe">Dissocier le groupe</button>
		</div>
		<% } %>
		<div>
			<button name="add_groupe">Ajouter un groupe</button>
		</div>
		<div>
			<input type="submit" name="add" value="Enregistrer" class="submit" />
		</div>
	</form>

<jsp:include page="/views/includes/footer.jsp" />